import {get,set } from 'lodash';
import stores from '../../..';
import setHAlign from '../../../excel/command/set-h-align';
import setVAlign from '../../../excel/command/set-v-align';
const removeDetailRow = (records, index, recordDeleteRow) => {
    const newRecords = [];
    if (index === -1) {
        newRecords.push({ detail_index: 1 });
    }
    let detailIndex = 1;
    records.forEach((record, recordIndex) => {
        if (recordIndex < index) {
            newRecords.push({ ...record, detail_index: detailIndex++ });
        } else if (recordIndex > index) {
            newRecords.push({ ...record, detail_index: detailIndex++ });
        } else {
            if (record.id) {
                recordDeleteRow(record.id);
            }
        }
    });
    return newRecords;
}
export default {
    canUndo: true,
    execute: (context, options, isUndo) => {
        context.suspendPaint();
        try {
            const { table, store, row: currentRow, sheet,sheetName,oldDelete ,deleteRowCount} = options;
            const bindingPath = table.bindingPath();
            if (isUndo) {
                //store.dataSourceInstance.setValue(bindingPath, options.oldValue);
                const {
                    deleteRowCount,
                    colCount,
                    rowCount,cRow,tableCol
                } = options.old;
                sheet.addRows(cRow,deleteRowCount);
                sheet.tables.resize(table,rowCount, colCount);

                options._oldSpans.forEach(range => {
                    sheet.addSpan(range.row, range.col, range.rowCount, range.colCount);
                });
                //store.dataSourceInstance =options._oldDataSource;
                options._oldDataSource.forEach((data) => {
                    store.dataSourceInstance.setValue(data.key, data.value);
                })
                
                // 回滚值
                for (let r= 0; r < deleteRowCount; r++) {
                    const row = cRow + r;
                    for (let c = 0; c < colCount; c++) {
                        const col = c + tableCol;
                        let cell;
                        cell = sheet.getCell(row, col);
                        console.log(options._oldValue[row][col]);
                        cell.cellType(options._oldCellTypes[row][col])
                            .backColor(options._oldBackColor[row][col])
                            .textIndent(options._oldTextIndent[row][col])
                            .backgroundImageLayout(options._oldBackgroundImageLayout[row][col])
                            .backgroundImage(options._oldBackGroundImage[row][col])
                            .bindingPath(options._oldBindingPath[row][col])
                            .font(options._oldFont[row][col])
                            .hAlign(options._oldHAlign[row][col])
                            .vAlign(options._oldVAlign[row][col])
                            .value(options._oldValue[row][col]);
                    }
                }

                
            } else {
                // const records = options.oldValue = store.dataSourceInstance.getValue(bindingPath);
                // const { row } = table.dataRange();
                // for(var i=deleteRowCount; i>0; i--){
                //     const records = store.dataSourceInstance.getValue(bindingPath);
                //     const newRecords = removeDetailRow(records, row > currentRow ? -1 : currentRow - row + i-1, (deleteId) => {
                //         const deletedRows = get(store.deletedRows, bindingPath);
                //         if (deletedRows) {
                //             deletedRows.push(deleteId);
                //         } else {
                //             set(store.deletedRows, bindingPath, [deleteId]);
                //         }
                //     });
                //     store.dataSourceInstance.setValue(bindingPath, newRecords);
                // }
                
                const { row: tableRow,col :tableCol,colCount, rowCount } = table.dataRange();
                const source = options._oldDataSource = store.dataSourceInstance.getSource()
                options.old = {
                    tableRow, tableCol, rowCount, colCount, bindingPath,cRow:currentRow,deleteRowCount
                    
                };
                options._oldBackColor = {};
                options._oldCellTypes = {};
                options._oldBackGroundImage = {};
                options._oldBackgroundImageLayout = {};
                options._oldTextIndent = {};
                options._oldValue = {};
                options._oldBindingPath = {};
                options._oldFont = {};
                options._oldHAlign = {};
                options._oldVAlign = {};
                options._oldSpans =[]
                options._oldDataSource = [];
                const data = [];
                for (let r = 0; r < deleteRowCount; r++) {
                    const row = r + currentRow;
                    if (!options._oldBackColor[row]) {
                        options._oldBackColor[row] = {}
                    }
                    if (!options._oldCellTypes[row]) {
                        options._oldCellTypes[row] = {};
                    }
                    if (!options._oldTextIndent[row]) {
                        options._oldTextIndent[row] = {};
                    }
                    if (!options._oldBackGroundImage[row]) {
                        options._oldBackGroundImage[row] = {};
                    }
                    if (!options._oldHAlign[row]) {
                        options._oldHAlign[row] = {};
                    }
                    if (!options._oldVAlign[row]) {
                        options._oldVAlign[row] = {};
                    }
                    if (!options._oldBackgroundImageLayout[row]) {
                        options._oldBackgroundImageLayout[row] = {};
                    }
                    if (!options._oldFont[row]) {
                        options._oldFont[row] = {};
                    }
                    if (!options._oldValue[row]) {
                        options._oldValue[row] = {};
                    }
                    if (!options._oldBindingPath[row]) {
                        options._oldBindingPath[row] = {};
                    }
                    for (let c = 0; c < colCount; c++) {
                        const col = tableCol + c;
                        const cell = sheet.getCell(row, col);
                        console.log(cell,cell.value(),cell.vAlign(),row, col,"值")
                        options._oldBackColor[row][col] = cell.backColor();
                        options._oldCellTypes[row][col] = cell.cellType();
                        options._oldTextIndent[row][col] = cell.textIndent();
                        options._oldBackGroundImage[row][col] = cell.backgroundImage();
                        options._oldBackgroundImageLayout[row][col] = cell.backgroundImageLayout();
                        options._oldFont[row][col] = cell.font();
                        options._oldValue[row][col] = cell.value();
                        options._oldVAlign[row][col] = cell.vAlign();
                        options._oldHAlign[row][col] = cell.hAlign();
                        const bingdingPath = options._oldBindingPath[row][col] = cell.bindingPath();

                        const span = sheet.getSpan(row, col);
                        if (span && span.row === row && span.col === col) {
                            if (!options._oldSpans) {
                                options._oldSpans = [];
                            }
                            options._oldSpans.push(span);
                        }

                        if(bingdingPath && r == 0) {
                            const cellType = cell.cellType();
                            if(cellType && cellType.expandType == '2') {

                            } else {
                                const strs = bingdingPath.split(".");
                                const path = strs[0] + "." + strs[1] + "." + strs[2];
                                options._oldDataSource.push({'key':path,'value': store.dataSourceInstance.getValue(path)});
                                
                                //data.push(source[strs[0]][strs[1]][strs[2]]);
                                data.push(strs)
                            }
                            
                        }

                    }
                }
                for(let i = 0 ; i < data.length ; i++){
                    const strs = data[i];
                    delete source[strs[0]][strs[1]][strs[2]]
                };

                const spans = [];
                for( let c = tableCol ; c < colCount + tableCol ; c++) {
                    const span = sheet.getSpan(currentRow, c);
                    if(span && span.row == currentRow && span.col == c ) {
                        spans.push(span);
                    }
                }
                // 删除逻辑
                sheet.deleteRows(currentRow,deleteRowCount);

                if(spans) {
                    for(let i = 0 ; i < spans.length ; i++) {
                        const row = spans[i].row;
                        const col = spans[i].col;

                        const span = sheet.getSpan(row,col);
                        if(!span) {
                            sheet.addSpan(row, col,spans[i].rowCount, spans[i].colCount);
                        }
                    }
                }

                sheet.tables.resize(table,rowCount - deleteRowCount, colCount);
            }
            // table.bindingPath(bindingPath);
            // store.calcAll(true, sheetName);
        } catch (e) {
            if (!!(window.console && window.console.log)) {
                window.console.log(e);
            }
        }
        context.resumePaint();
    }
};